package com.og.ouroa.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.og.ouroa.pojo.entity.CourseFile;
import com.og.ouroa.mapper.CourseFileMapper;
import com.og.ouroa.pojo.vo.CourseFileVO;
import com.og.ouroa.service.CourseFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Us
 * @since 2021-12-13
 */
@Service
public class CourseFileServiceImpl extends ServiceImpl<CourseFileMapper, CourseFile> implements CourseFileService {




    @Override
    public CourseFileVO insert(CourseFileVO vo) {

        CourseFile courseFile = new CourseFile();
        BeanUtils.copyProperties(vo,courseFile);

        baseMapper.insert(courseFile);
        vo.setId(courseFile.getId());
        return vo;

    }


    @Override
    public String reomveByName(String fileName, Integer cid) {

        QueryWrapper<CourseFile> wrapper = new QueryWrapper<>();
        wrapper.eq("file_name",fileName);
        wrapper.eq("course_id",cid);
        CourseFile courseFile = baseMapper.selectOne(wrapper);
        baseMapper.delete(wrapper);
        return courseFile.getFile();

    }


    @Override
    public String reomveFid(Integer fid) {
        QueryWrapper<CourseFile> wrapper = new QueryWrapper<>();
        wrapper.eq("id",fid);
        CourseFile courseFile = baseMapper.selectOne(wrapper);
        baseMapper.delete(wrapper);
        return courseFile.getFile();
    }

    @Override
    public IPage<CourseFile> listBySid(Page<CourseFile> pages, Integer sid) {
        QueryWrapper<CourseFile> wrapper = new QueryWrapper<>();
        wrapper.eq("sid",sid);
        return baseMapper.selectPage(pages,wrapper);
    }

    @Override
    public IPage<CourseFile> listByCid(Page<CourseFile> pages, Integer cid) {
        QueryWrapper<CourseFile> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",cid);
        return baseMapper.selectPage(pages,wrapper);
    }
}
